﻿/*
 * QWine Queue Machine (Special build for Immigration Office)
 * Copyright (C) 2010 Wish Enterprise Development Team

 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 * 
 * Contact Us:
 * E-Mail   : contact@wishenterprise.com     Website: http://www.wishenterprise.com
 * 
 * */

using System;
using System.Windows.Forms;
using SmsEntity.Local;
using SmsRepository;
using SmsRepository.Local;

namespace SmsManualRespond
{
    public partial class PasswordUI : Form
    {
        private readonly User _user;

        public PasswordUI(User user)
        {
            _user = user;
            InitializeComponent();
        }

        private void PasswordUI_Shown(object sender, EventArgs e)
        {
            if (!AuthenticateUser(this))
            {
                Close();
                Dispose();
            }
        }

        /// <summary>
        /// Authenticates the user.
        /// </summary>
        /// <param name="parent">The parent.</param>
        /// <returns></returns>
        public bool AuthenticateUser(IWin32Window parent)
        {
            DialogResult dlg;
            using (var f = new LogonPresentation())
            {
                dlg = f.ShowDialog(parent);
                f.SetUser(_user.UserId);
            }

            return (dlg.Equals(DialogResult.OK));
        }

        /// <summary>
        /// Handles the Click event of the btnSave control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void btnSave_Click(object sender, EventArgs e)
        {
            if ((!string.IsNullOrEmpty(txtPassword.Text.Trim())) |
                (!string.IsNullOrEmpty(txtConfirmPassword.Text.Trim())))
            {
                if (txtPassword.Text.Trim().Length < 6)
                {
                    MessageBox.Show(@"Password minimum 6 characters!", @"Password",
                                    MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                if (!txtPassword.Text.Trim().Equals(txtConfirmPassword.Text.Trim()))
                {
                    MessageBox.Show(@"Confirm password does not match!", @"Password",
                                    MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
            }

            // --- EMPTY PASSWORD
            if (string.IsNullOrEmpty(txtPassword.Text.Trim())) return;

            _user.Password =
                UserCryptoService.Encrypt(
                    _user.UserId,txtPassword.Text.Trim());

            var userDao = new UserBase();
            MessageBox.Show(
                userDao.Update(_user) == 1
                    ? @"Your password has been successfully changed!"
                    : @"Change password failed!", @"Information",
                MessageBoxButtons.OK, MessageBoxIcon.Information);

            Close();
            Dispose();

        }

        /// <summary>
        /// Handles the TextChanged event of the txtPassword control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void txtPassword_TextChanged(object sender, EventArgs e)
        {
            ((TextBox)sender).Text = EnvironmentRepository.SuppressChar(((TextBox)sender).Text);
        }

        /// <summary>
        /// Handles the TextChanged event of the txtConfirmPassword control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private void txtConfirmPassword_TextChanged(object sender, EventArgs e)
        {
            ((TextBox)sender).Text = EnvironmentRepository.SuppressChar(((TextBox)sender).Text);
        }
    }
}